﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TeamsPortal.DataAccess;
using TeamsPortal.ServiceClient;

namespace TeamsPortal.Tests.DataAccess
{
    internal static class DATestsHelper
    {
        public static readonly string UnitTestUserIP = "127.0.0.28";

        public static User CreateUser(string userName, UserRole role)
        {
            var result = new User(0, "UNITTEST USERNAME FULL")
            {
                Role = role,
                BirthDate = new DateTime(1960, 12, 31),
                ExternalProfileUrl = "http://url.com/",
                //Photo = "http://url.com/",
                Profile = "Hello world1",
                Signature = "Hello world2",
                TimeZone = new TimeSpan(2, 0, 0),
                Website = "http://url.com/",
            };
            string providerId = "00" + new Random().Next(int.MaxValue / 2, int.MaxValue).ToString();

            result = new UsersServiceClient(null).Add(result, AuthenticationProvider.Membership, providerId);

            return result;
        }

        public static ForumCategory CreateOrGetAnyCategory()
        {
            ForumCategory result = null;

            List<ForumCategory> categories = new ForumsServiceClient().GetCategories();
            if (categories.Count == 0)
            {
                new CategoriesDataAccess().Add(new ForumCategory() { Name="UnitTests", Order = 1 });
                categories = new ForumsServiceClient().GetCategories();
            }
            result = categories[0];

            return result;
        }

        public static Forum CreateForum(User user, ForumCategory category)
        {
            var da = new ForumsDataAccess();
            string shortName = da.GetAvailableShortName("UTForum");
            var f = new Forum()
            {
                Name = "UTForum",
                ShortName = shortName,
                Description = "description",
                Category = category,
                ReadAccessRole = UserRole.Member,
                PostAccessRole = UserRole.Member
            };

            da.Add(f, user.Id);
            f = da.GetByShortName(shortName);

            return f;
        }

        public static Forum CreateForum(User user)
        {
            Forum result = CreateForum(user, CreateOrGetAnyCategory());

            return result;
        }

        public static Topic CreateTopic(Forum forum, User user, string shortName)
        {
            var topic = new Topic()
            {
                Title = "Title",
                ShortName = shortName,
                Description = "Description",
                User = user,
                Tags = new TagList() { "tag" },
                Forum = forum,
                ReadAccessRole = UserRole.Member,
                PostAccessRole = UserRole.Member
            };

            var da = new TopicsDataAccess();
            da.Add(topic, UnitTestUserIP);

            return topic;
        }

        public static Topic CreateTopic(Forum forum, User user)
        {
            var shortName = "UTTopicShortName";
            Topic topic = CreateTopic(forum, user, shortName);

            return topic;
        }

        public static void CreateMessage(Topic topic, User user)
        {
            var message = new Message()
            {
                Body = "hello",
                Date = DateTime.Now,
                Topic = topic,
                User = user,
            };

            new MessagesServiceClient().Add(message, UnitTestUserIP);
        }

        public static ForumCategory CreateCategory(string name)
        {
            ForumCategory result = new CategoriesDataAccess().Add(new ForumCategory() { Name = name, Order = 1 });
            
            return result;
        }
    }
}
